package cn.hfatec.shms.web.yk.ykTjmx.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.web.xc.xcTjmxb.model.XcTjmxbParams;
import cn.hfatec.shms.web.yk.ykTjmx.model.YkTjmx;
import cn.hfatec.shms.web.yk.ykTjmx.model.YkTjmxParams;
import cn.hfatec.shms.web.yk.ykTjmx.service.YkTjmxService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/yk/ykTjmx")
public class YkTjmxAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "YkTjmxService")
    private YkTjmxService ykTjmxService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(YkTjmxParams ykTjmxParams, Page page, Model model) {

        Table table = ykTjmxService.queryYkTjmxTable(ykTjmxParams, page);
        YkTjmx ykTjmx = ykTjmxService.hjYkTjmx(ykTjmxParams);
        model.addAttribute("ykTjmxsTable", table);
        model.addAttribute("ykTjmxHj", ykTjmx);
        model.addAttribute("ykTjmxParams", ykTjmxParams);
        return "/yk/ykTjmx/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(YkTjmxParams ykTjmxParams, Page page, Model model) {
        try {
            list(ykTjmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykTjmx/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
    /**
     * 查询列表
     */
    @RequestMapping("/alist")
    public String alist(YkTjmxParams ykTjmxParams, Page page, Model model) {

        Table table = ykTjmxService.queryYkTjmxTableA(ykTjmxParams, page);

        model.addAttribute("ykTjmxsTable", table);

        model.addAttribute("ykTjmxParams", ykTjmxParams);
        return "/yk/ykTjmx/alist";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxAList")
    @ResponseBody
    public String ajaxAList(YkTjmxParams ykTjmxParams, Page page, Model model) {
        try {
            alist(ykTjmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykTjmx/atable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/dorefreshtj")
    @ResponseBody
    public String dorefreshtj(YkTjmxParams ykTjmxParams, Page page, Model model) {

        try {
            alist(ykTjmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykTjmx/AselectList", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/ajaxSelectList")
    @ResponseBody
    public String ajaxSelectList(YkTjmxParams ykTjmxParams, Model model, Page page) {
        try {
            alist(ykTjmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykTjmx/AselectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            YkTjmx ykTjmx = new YkTjmx();
            model.addAttribute("ykTjmx", ykTjmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykTjmx/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            YkTjmx ykTjmx = ykTjmxService.getYkTjmx(id);
            model.addAttribute("ykTjmx", ykTjmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/yk/ykTjmx/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteYkTjmx(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            ykTjmxService.deleteYkTjmxBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveYkTjmx(@Valid YkTjmx ykTjmx, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            ykTjmxService.saveYkTjmx(ykTjmx);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据YkTjmx保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(YkTjmxParams ykTjmxParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            ykTjmxService.exportExcel(ykTjmxParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
